/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.plugin;

import java.awt.event.ActionListener;
import java.util.Calendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mopore.jat.core.dashboard.DashboardInfo;
import org.mopore.jat.core.messaging.Message;
import org.mopore.jat.gui.IThemeChangeListener;
import org.mopore.jat.gui.Theme;
import org.mopore.jat.plugin.IJatPlugin.PluginLoadState;

public class PluginContextWrapperAgent implements IPluginContext {
	
	private final IPluginContextInternal pluginContext;
	
	private final Log log = LogFactory.getLog(getClass().getName());
	
	private final String pluginName;
	
	private final IPluginFileAccess pluginFileAccess;

	public PluginContextWrapperAgent(IPluginContextInternal pluginContext, IJatPluginInternal plugin) throws PluginException {
		this.pluginContext = pluginContext;
		this.pluginName = plugin.getPluginName();
		this.pluginFileAccess = new PluginFileAccessJaxB( plugin.getClass() );
	}

	public void addPluginFocusListener(IPluginFocusListener pluginFocusListener, String... pluginNames) {
		log.trace( pluginName + " is calling: addPluginFocusListener" );
		pluginContext.addPluginFocusListener( pluginFocusListener, pluginNames );
	}


	public void addTerminatedListener(String pluginName, ITerminatedListener terminatedListener) {
		log.trace( pluginName + " is calling: addTerminatedListener" );
		pluginContext.addTerminatedListener( pluginName, terminatedListener);
	}

	public void addThemeChangeListener(IThemeChangeListener themeChangeListener) {
		log.trace( pluginName + " is calling: addThemeChangeListener" );
		pluginContext.addThemeChangeListener( themeChangeListener );
	}

	public void addMouseActionListener(IMouseActionListener mouseActionListener, String... pluginNames) {
		log.trace( pluginName + " is calling: addTogoActionListener" );
		pluginContext.addMouseActionListener( mouseActionListener, pluginNames );
	}


	public PluginLoadState getPluginLoadState(String pluginName) {
		log.trace( pluginName + " is calling: getPluginLoadState" );
		PluginLoadState loadState = pluginContext.getPluginLoadState( pluginName );
		return loadState;
	}

	public IJatPlugin getPlugin(String name) {
		log.trace( pluginName + " is calling: getPlugin" );
		IJatPlugin plugin = pluginContext.getPlugin( name );
		return plugin;
	}

	public String getPluginNameWithFocus() {
		log.trace( pluginName + " is calling: getPluginNameWithFocus" );
		String pluginName = pluginContext.getPluginNameWithFocus();
		return pluginName;
	}

	public IJatPlugin getPluginWithFocus() {
		log.trace( pluginName + " is calling: getPluginWithFocus" );
		IJatPlugin plugin = pluginContext.getPluginWithFocus();
		return plugin;
	}
	

	public IPluginFileAccess getFileAccess() {
		log.trace( pluginName + " is calling: getFileAccess" );
		return pluginFileAccess;
	}

	@Override
	public Theme getCurrentTheme() {
		log.trace( pluginName + " is calling: getCurrentTheme" );
		return pluginContext.getCurrentTheme();
	}

	@Override
	public void updateTrayIcon(String value) {
		pluginContext.updateTrayIcon(value);
	}

	/**
	 * This method has been overridden.
	 * @param message
	 * @see org.mopore.jat.plugin.IPluginContext#showMessage(org.mopore.jat.core.messaging.Message)
	 */
	@Override
	public void showMessage(Message message) {
		pluginContext.showMessage( message );
	}

	/**
	 * This method has been overridden.
	 * @param message
	 * @param messageAction
	 * @see org.mopore.jat.plugin.IPluginContext#showMessage(org.mopore.jat.core.messaging.Message, java.awt.event.ActionListener)
	 */
	@Override
	public void showMessage(Message message, ActionListener messageAction) {
		pluginContext.showMessage( message, messageAction );
	}

	/**
	 * This method has been overridden.
	 * @return
	 * @see org.mopore.jat.plugin.IPluginContext#hasFocus()
	 */
	@Override
	public boolean hasFocus() {
		String pluginName = getPluginNameWithFocus();
		boolean hasFocus = this.pluginName.equals( pluginName );
		return hasFocus;
	}

	/**
	 * This method has been overridden.
	 * @return
	 * @see org.mopore.jat.plugin.IPluginContext#getWindowController()
	 */
	@Override
	public IWindowController getWindowController() {
		IWindowController windowController = 
			pluginContext.getWindowController();
		return windowController;
	}

	/**
	 * This method has been overridden.
	 * @see org.mopore.jat.plugin.IPluginContext#pinDashboardInfo()
	 */
	@Override
	public void pinToDashboard( String title, String text, Calendar validThru ) {
		
		long id = System.currentTimeMillis();
		DashboardInfo dashboardInfo = new DashboardInfo();
		dashboardInfo.setId( id );
		dashboardInfo.setTitle( title );
		dashboardInfo.setText( text );
		dashboardInfo.setValidTill( validThru );
		
		pluginContext.pinDashboardInfo( dashboardInfo, pluginName );
	}

	/**
	 * This method has been overridden.
	 * @see org.mopore.jat.plugin.IPluginContext#showDashboard()
	 */
	@Override
	public void showDashboard() {
		pluginContext.showDashboard();
	}

	
}
